#!/bin/bash

source /home/shell/util.sh



# 安装函数
function install () {

    if [ ! -f "/usr/sbin/nginx" ]; then
        bash /home/shell/ubuntu/nginx-src-install.sh
    fi
    if [ ! -f "/usr/local/bin/php" ]; then
        bash /home/shell/php-install.sh
    fi

    requireMysqlCient


    while [ true ]; do
        printInfo "phpMySQLAdmin版本列表："
        curl -sL https://www.phpmyadmin.net/files/|grep "<th><a href"|perl -pe "s/.*\/\">(.*?)<\/a>.*/\1/g"|head -5
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
        printFail "请输入版本"
        else
        break;
        fi
    done
    
    download /opt/phpMyAdmin-all-languages.tar.gz https://files.phpmyadmin.net/phpMyAdmin/${version}/phpMyAdmin-${version}-all-languages.tar.gz

    tar -xf /opt/phpMyAdmin-all-languages.tar.gz -C /opt
    rm -rf /opt/phpMyAdmin-all-languages.tar.gz
    mv /opt/phpMyAdmin-*-all-languages/ /opt/phpMyAdmin
}

# 配置函数
function config(){

    if [ ! -d "/opt/phpMyAdmin/tmp" ]; then
        mkdir -p /opt/phpMyAdmin/tmp
        chmod 777 /opt/phpMyAdmin/tmp
    fi

    read -p $'认证类型:\n1. config\n2. cookie\n[config]:' auth_type;
    auth_type=${auth_type:-"config"}

    if [[ "${auth_type}" == "config" ]]; then
        read -p "MySQL HOST[127.0.0.1]：" mysql_host;
        mysql_host=${mysql_host:-"127.0.0.1"}
        read -p "MySQL HOST[3306]：" mysql_port;
        mysql_port=${mysql_port:-"3306"}
        read -p "MySQL USER[root]：" mysql_user;
        mysql_user=${mysql_user:-"root"}
        read -p "MySQL USER[123456]：" mysql_passwd;
        mysql_passwd=${mysql_passwd:-"123456"}
        sed -ri "s/\['host'\]\s?=\s?'localhost';/['host'] = '"${mysql_host}"';/g" /opt/phpMyAdmin/libraries/config.default.php
        sed -ri "s/\['user'\]\s?=\s?'root';/['user'] = '"${mysql_user}"';/g" /opt/phpMyAdmin/libraries/config.default.php
        sed -ri "s/\['password'\]\s?=\s?'';/['password'] = '"${mysql_passwd}"';/g" /opt/phpMyAdmin/libraries/config.default.php
        sed -ri "s/\['auth_type'\]\s?=\s?'cookie';/['auth_type'] = 'config';/g" /opt/phpMyAdmin/libraries/config.default.php
        if [[ "${mysql_port}" != "3306" ]]; then
            sed -ri "s/\['port'\]\s?=\s?'';/['port'] = '"${mysql_port}"';/g" /opt/phpMyAdmin/libraries/config.default.php
        fi

expect <<EOF
    spawn mysql_config_editor set --login-path=phpMyAdmin-dev --host=${mysql_host} --port=${mysql_port} --user=${mysql_user} --password
    expect {
        "password" {send "$mysql_passwd\n";exp_continue}
        "exists" {send "y\n";interact}
    }
EOF

mysql --login-path=phpMyAdmin-dev<<EOF
source /opt/phpMyAdmin/sql/create_tables.sql;
EOF
    fi

    sed -ri "s/\['AllowArbitraryServer'\]\s?=\s?false;/['AllowArbitraryServer'] = true;/g" /opt/phpMyAdmin/libraries/config.default.php
    sed -ri "s/\['blowfish_secret'\]\s?=\s?'';/['blowfish_secret'] = 'X1MfDT7rAcHSlHDVwYcWfVtr73RkRmG4';/g" /opt/phpMyAdmin/libraries/config.default.php
    
}

function run () {
    nginx_conf=`nginx -t 2>&1|grep test|perl -pe "s/.*file\s?(.*?)\stest.*/\1/g"`;
    printInfo "============================phpMyAdmin============================"
    printInfo "请修改nginx配置：${nginx_conf}"
    printInfo "root     /opt/phpMyAdmin;\n"
    printInfo "location / {"
    printInfo "    index  index.php index.htm;"
    printInfo "}"
    printInfo "location ~* \.php\$ {"
    printInfo "    fastcgi_index   index.php;"
    printInfo "    fastcgi_pass    127.0.0.1:9000;"
    printInfo "    include         fastcgi_params;"
    printInfo "    fastcgi_param   SCRIPT_FILENAME    \$document_root\$fastcgi_script_name;"
    printInfo "    fastcgi_param   SCRIPT_NAME        \$fastcgi_script_name;"
    printInfo "}"
# root /opt/phpMyAdmin;

# location / {
#     index  index.php index.htm;
# }

# location ~* \.php$ {
#     fastcgi_index   index.php;
#     fastcgi_pass    127.0.0.1:9000;
#     include         fastcgi_params;
#     fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
#     fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
# }
    printInfo "============================phpMyAdmin============================"
}

# 运行安装和配置
install && config && run